package com.wxmit.module.system.web;

import cn.hutool.core.lang.tree.Tree;
import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.web.operatelog.Log;
import com.wxmit.framework.web.operatelog.enums.BusinessType;
import com.wxmit.base.validate.AddGroup;
import com.wxmit.base.validate.UpdateGroup;
import com.wxmit.module.system.domain.SysDept;
import com.wxmit.module.system.service.ISysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  部门管理
 * @Author huangxiong
 * @Date 2022/7/21 17:16
 * @Version V1.0
 */
@RestController
@RequestMapping("/sys/dept")
@Tag(name="部门管理")
public class SysDeptController {

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     *  列出部门树
     * @return 部门树
     */
    @GetMapping("/tree")
    @Operation(summary = "获取部门树")
    public List<Tree<Long>> listTree(){
        return sysDeptService.listTree();
    }

    /**
     *  根据id获取对象
     * @param id 部门id
     * @return 部门对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id获取部门对象")
    @Parameters(
            @Parameter(name = "id")
    )
    public SysDept info(@PathVariable("id") Long id){
        return sysDeptService.getById(id);
    }

    /**
     *  增加部门
     * @param sysDept 部门对象
     * @return 成功结果
     */
    @PostMapping
    @Log(module="部门管理",name="新增部门",type = BusinessType.INSERT)
    public ApiResult add(@RequestBody @Validated(AddGroup.class) SysDept sysDept){
        return ApiResult.successWithData(sysDeptService.save(sysDept));
    }

    /**
     *  更新部门
     * @param sysDept 部门对象
     * @return 成功结果
     */
    @PutMapping
    @Log(module="部门管理",name="更新部门",type = BusinessType.UPDATE)
    public ApiResult update(@RequestBody @Validated(UpdateGroup.class) SysDept sysDept){
        return ApiResult.successWithData(sysDeptService.updateById(sysDept));
    }

    /**
     *  批量删除
     * @param ids ids
     * @return 成功结果
     */
    @DeleteMapping("/{ids}")
    @Log(module="部门管理",name="删除部门",type = BusinessType.DELETE)
    public ApiResult remove(@PathVariable List<Long> ids){
        return ApiResult.successWithData(sysDeptService.removeByIds(ids));
    }
}
